package thread;

class Counter {
    Object locker = new Object();
    public int count = 0;

    public void add() {
        synchronized (locker) {
            count++;
        }
    }

    synchronized public static void fun() {
        System.out.println("synchronized--修饰静态方法");
    }

    public int getCount() {
        return count;
    }
}

public class Demo21 {
    public static void main(String[] args) throws InterruptedException {

        Counter counter = new Counter();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                counter.add();
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                counter.add();
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t1.join();

        System.out.println(counter.getCount());
    }
}
